package homework.day06;

import homework.day06.InputUtil.InputField;

import java.io.Serializable;

/**
 * 设计一个类:User
 * 里面有四个属性:String name,String pwd,String nick,int age
 * 定义get,set方法以及toString方法和构造方法
 */
public class User implements Serializable {
    @InputField(title = "用户名", regex = "\\w{1,32}", notNull = true)
    private String name;
    @InputField(title = "密码", notNull = true)
    private String pwd;
    @InputField(title = "昵称", notNull = true)
    private String nick;
    @InputField(title = "年龄", notNull = true)
    private int age;

    public User() {
    }

    public User(String name, String pwd, String nick, int age) {
        this.name = name;
        this.pwd = pwd;
        this.nick = nick;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        if (name.length() == 0 || !name.matches("\\w{1,32}")) {
            System.out.println("用户名不符合要求");
            return;
        }
        this.name = name;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        if (pwd.length() == 0) {
            System.out.println("密码不能为空");
            return;
        }
        this.pwd = pwd;
    }

    public String getNick() {
        return nick;
    }

    public void setNick(String nick) {
        if (nick.length() == 0) {
            System.out.println("昵称不能为空");
            return;
        }
        this.nick = nick;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if (!(age > 0) || age > 100) {
            System.out.println("年龄输入错误");
            return;
        }
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", pwd='" + pwd + '\'' +
                ", nick='" + nick + '\'' +
                ", age=" + age +
                '}';
    }
}
